#ifndef SEMAPHORE_H
#define SEMAPHORE_H

#include <pthread.h>

//#include "bcti_NonCopyable.h"


namespace lzpLib{
class Semaphore{//: private NonCopyable{
public:
    /**
     * Creates a new semaphore. Optionally specify an initial count,
     * default is 0. 
     */
    Semaphore(unsigned int initialCount=0);

    /**
     * Destroys this semaphore.
     */
    ~Semaphore();

    /**
     * Decrements the semaphore count. if it is not positive waits 
     * until it is positive and then decrements the count.
     */
    void wait();

    /**
     * Decrements the semaphore count and if the count is positive 
     * returns true;if not, returns false. Does not block. 
     */
    bool tryWait();

    /**
     * Decrements the semaphore count, if it is not positive, waits 
     * until it is positive and then decrements the count and 
     * returns true. Gives up after milliseconds and returns false 
     * instead. 
     */
    int timedWait(unsigned int milliSeconds);

    /**
     * Increments the semaphore count.If it reached zero, marks a 
     * thread currently waiting on this semaphore as ready-to-run. 
     * If more than 1 threads are waiting, only one that not 
     * specified will be woken. Never blocks. 
     */
    void post();

    /**
     * Return internal count. for debug/test.
     */
   unsigned int getCount(void) const {return count_;}

private:
    pthread_mutex_t mutex_;
    pthread_cond_t cond_;
    unsigned int count_;
};
}

#endif // SEMAPHORE_H

